import { ref, reactive } from 'vue';
import axios from '../utils/api';
import { BASE_URL } from '../consts/index'

export function useAxios(url, method = 'post') {
    const loading = ref(false);
    const data = ref(null);

    const fn = async (params = {}, headers = {}) => {
        loading.value = true;
        uni.showLoading({ title: '加载中' })
        try {
            const response = await axios({
                baseURL: BASE_URL,
                method,
                url,
                ...(method.toLowerCase() === 'get' ? { params } : { data: params }),
                headers,
            });
            if (response.data.success) {
                data.value = response.data.data;
                return response.data.data;
            } else {
                throw new Error(response.data.msg)
            }
        } finally {
            loading.value = false;
            uni.hideLoading()
        }
    };

    return [fn, {data, loading}]
}